﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Web.Security;

public partial class account_Favoriteshop : System.Web.UI.Page
{
    private List<Shop> favoriteShops;

    protected void Page_Load(object sender, EventArgs e)
    {
        getFavoriteShop();
        //string userId = Membership.GetUser().ProviderUserKey.ToString();
    }

 

    public class Shop
    {
        /// <summary>
        /// 直接读取给定的SqlDataReader来构造自身。注：不会调用reader.Read()。
        /// </summary>
        /// <param name="reader"></param>

        public string Name { get; set; }

        public string Description { get; set; }

        public int? CoverPhoto { get; set; }

        public string Category { get; set; }
    }

    private void getFavoriteShop()
    {
        var connString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ASPNETDBConnectionString"];
        if (connString == null)
            throw new ArgumentNullException("无法获取指定的数据库连接字符串。");
        string userId = Membership.GetUser().ProviderUserKey.ToString();

        using (SqlConnection connection = new SqlConnection(connString.ConnectionString))
        {
            connection.Open();

            SqlCommand command = new SqlCommand(
                string.Format(@"SELECT   Shop.category, Shop.Description, [Favorite Shop].UserId, [Favorite Shop].shopName
                                FROM      Shop INNER JOIN
                                            [Favorite Shop] ON Shop.name = [Favorite Shop].shopName
                                WHERE   ([Favorite Shop].UserId = '{0}')", userId),
                connection);
            command.CommandType = System.Data.CommandType.Text;
            var reader = command.ExecuteReader();

            favoriteShops = new List<Shop>();
            while (reader.Read())
            {
                favoriteShops.Add(new Shop
                {
                    Name = reader.GetString(3),
                    Description = reader.GetString(1),
                    Category = reader.GetString(0),
                });
            }
        }
    }
    protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        e.Result = favoriteShops;
    }
}